import { WorldUIPool } from './UIPool';
import { util } from './Util';

export const PlayerHeadUIPool: WorldUIPool<PlayerHeadUI> = new WorldUIPool(() => { return new PlayerHeadUI(); });

export class PlayerHeadUI {
    uiWidget: mw.UIWidget;
    stage: boolean;
    targetCharacter: Character;

    txt_name: TextBlock;
    img_vip: Image;
    img_rank_icon: Image;
    txt_rank_exp: TextBlock;

    constructor() {
        this.uiWidget = GameObject.spawn('UIWidget', { replicates: false });
        this.uiWidget.onDestroyDelegate.add(() => {
            this.targetCharacter = null;
        });
        this.uiWidget.setUIbyID('C13E19574DD01D16CFBCDAB5A679AF3E');
        this.uiWidget.widgetSpace = WidgetSpaceMode.OverheadUI;
        this.uiWidget.occlusionEnable = false;
        this.uiWidget.hideByDistanceEnable = false;

        this.txt_name = <TextBlock>this.uiWidget.getTargetUIWidget().rootContent.getChildByName('text_Name');
        this.img_vip = <Image>this.uiWidget.getTargetUIWidget().rootContent.getChildByName('img_vip');
        this.img_rank_icon = <Image>this.uiWidget.getTargetUIWidget().rootContent.getChildByName('con_rank/img_rank_icon');
        this.txt_rank_exp = <TextBlock>this.uiWidget.getTargetUIWidget().rootContent.getChildByName('con_rank/txt_rank_exp');
    }

    setInfo(player: Character, name: string, hasVIP: boolean, rankExp: number) {
        // 绑定
        this.targetCharacter = player;
        player.attachToSlot(this.uiWidget, HumanoidSlotType.Root);
        if (!this.uiWidget.localTransform) return;
        this.uiWidget.localTransform.position = new Vector(0, 0, 200);
        // 名称
        this.txt_name.text = name;
    }
}
